<?php
// +----------------------------------------------------------------------
// | 众联TK架构 [ ZoomTk ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2037 成都众联思索科技有限公司 [ http://www.zoomtk.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.sopvip.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\mall\common;
require 'ZoomTK.php';
class Base extends \think\Controller {
	public function _initialize() {
		parent::_initialize();
		//设置语言
		$this->lang();
		//主题设置
		$this->setThemes();
		// 判断是否登录，并定义用户ID常量
		defined('UID') or define('UID', $this->isLogin());

	}
	/**
	 *多模板设置
	 */
	protected function setThemes() {
		$module = $this->request->module();
		$themes['mobile'] = config('mobile_themes') ? config('mobile_themes') : 'mobile';
		$themes['pc']     = config('pc_themes') ? config('pc_themes') : 'default';
		$view_path        = isMobile() ? 'template/' . $themes['mobile'] . '/' : 'template/' . $themes['pc'] . '/';
		$this->view->config('view_path', $view_path .  $module . '/')
			->config('tpl_replace_string',array(
				'__IMG__' => BASE_PATH . '/' . $view_path . 'static/images',
				'__JS__'  => BASE_PATH . '/' . $view_path . 'static/js',
				'__CSS__' => BASE_PATH . '/' . $view_path . 'static/css',
			));
	}

	protected function lang() {
		$lang=input('lang')?input('lang'):'';
		switch ($lang) {
			case 'cn':
				cookie('think_var', 'zh-cn');
				break;
			case 'en':
				cookie('think_var', 'en-us');
				break;
			default:
				cookie('think_var', \think\lang::detect());
		}
	}


	/**
	 * 检查是否登录，没有登录则跳转到登录页面
	 * @author Sunday <153788231@qq.com>
	 * @return int
	 */
	final protected function isLogin()
	{
		//检查是否需要登录
		if ($uid = is_login()) {
			// 已登录
			return $uid;
		} else {
			// 未登录
			cookie('__url_return__', $_SERVER['REQUEST_URI']);
			$this->redirect('member/login/login');
		}
	}
}
